package edu.hitsz.application;

import edu.hitsz.ScoreRank;
import edu.hitsz.StartGame;
import edu.hitsz.scorelist.ScoreDao;
import edu.hitsz.scorelist.ScoreDaoImpl;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * 程序入口
 * @author hitsz
 */
public class Main {

    public static final int WINDOW_WIDTH = 512;
    public static final int WINDOW_HEIGHT = 768;
    public static Object object = new Object();

    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {

        System.out.println("Hello Aircraft War");

        // 获得屏幕的分辨率，初始化 Frame
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("Aircraft War");
        frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setResizable(false);
        //设置窗口的大小和位置,居中放置
        frame.setBounds(((int) screenSize.getWidth() - WINDOW_WIDTH) / 2, ((int) screenSize.getHeight() - WINDOW_HEIGHT) / 2,
                WINDOW_WIDTH, WINDOW_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setContentPane(new StartGame().getMain());
        frame.setVisible(true);

        synchronized (object) {
            try {
                object.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        frame.getContentPane().removeAll();

        Game game = (Game) Class.forName(StartGame.selectPattern).newInstance();
        frame.setContentPane(game);
        game.action();
        frame.setVisible(true);

        synchronized (object) {
            try {
                object.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        ScoreRank scoreRank = new ScoreRank();
        JPanel panel = scoreRank.getScoreRank();
        frame.setContentPane(panel);
        frame.setVisible(true);

        String m = JOptionPane.showInputDialog("游戏结束，您的得分为" + Game.score + "\n请输入名字记录得分：");
        System.out.println(m);
        ScoreDao scoreDao = new ScoreDaoImpl();
        if (m != null) {
            try {
                scoreDao.scoreadd(m, Game.score);
            } catch (IOException e) {
                e.printStackTrace();
            }
            scoreRank.update();
        }
        scoreDao.getscorelist();
    }
}
